<?php

namespace App\Crontab;

use App\Game\Core\Djs;
use Hyperf\Di\Annotation\Inject;
use Hyperf\Crontab\Annotation\Crontab;
use Hyperf\Contract\ConfigInterface;
use Hyperf\Logger\LoggerFactory;
use Psr\Log\LoggerInterface;

/**
 * @Crontab(name="clean_room", rule="* 3 * * *", callback="execute", memo="每天03:00定时清空所有房间")
 */
class CleanRoom
{
    /**
     * @var LoggerInterface
     */
    protected $logger;

    /**
     * @Inject
     * @var ConfigInterface
     */
    protected $config;

    public function __construct(LoggerFactory $loggerFactory)
    {
        $this->logger = $loggerFactory->get('clean', 'default');
    }

    public function execute()
    {
        Djs::cleanDjsData();
        $redis = redis();
        $game_conf = $this->config->get('game');
        $total_room_key = $game_conf['user_room_total'];
        $total_room = $redis->sMembers($total_room_key);
        if (!empty($total_room)) {
            foreach ($total_room as $room_no) {
                @clearRoomDataAll($room_no);
            }
        }
        $this->logger->warning('清空房间 --' . date('Y-m-d H:i:s', time()));
    }


}